package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private  BrandMapper brandMapper;

    /**
     * 带条件分页查询的方法
     * @param currentPage
     * @param pageSize
     * @param brand
     * @return
     */
    @Override
    public Page selectByPage(int currentPage, int pageSize, Brand brand) {
        //设置分页参数
        Page page=new Page(currentPage,pageSize);
        //创建LambdaQueryWrapper条件对象，设置查询条件
        LambdaQueryWrapper<Brand> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(brand.getStatus()!=null,Brand::getStatus,brand.getStatus())
                .like(brand.getBrandName()!=null && brand.getBrandName().length()>0,
                        Brand::getBrandName,brand.getBrandName())
                .like(brand.getCompanyName()!=null && brand.getCompanyName().length()>0,
                        Brand::getCompanyName,brand.getCompanyName());
        //执行操作
        brandMapper.selectPage(page,lambdaQueryWrapper);
        //封装分页结果
        return page;
    }
}
